package com.GroupSix.controller;

import com.GroupSix.entity.ExamAnswerEntity;
import com.GroupSix.service.ExamAnswerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/exam")
@Slf4j
public class ExamAnswerController {
    @Autowired
    private ExamAnswerService examAnswerService;
    //提交试卷
    @PostMapping("/submit")
    public Map<String, Object> submitExam(@RequestBody ExamAnswerEntity answer) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 先检查考试时间
            Map<String, Object> timeCheck = examAnswerService.checkExamTime(answer.getExamId());
            if ("已结束".equals(timeCheck.get("examStatus"))) {
                result.put("code", 403);
                result.put("msg", "考试已结束，无法提交试卷");
                return result;
            }

            // 提交试卷
            examAnswerService.submitExam(answer);
            result.put("code", 0);
            result.put("msg", "提交成功");
            result.put("data", answer.getScore()); // 返回考试分数

        } catch (Exception e) {
            log.error("提交试卷失败", e);
            result.put("code", 500);
            result.put("msg", "提交失败：" + e.getMessage());
        }
        return result;
    }

    //获取考试时间状态
    @GetMapping("/time/{examId}")
    public Map<String, Object> getExamTime(@PathVariable Integer examId) {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> timeInfo = examAnswerService.checkExamTime(examId);
            result.put("code", 0);
            result.put("msg", "success");
            result.put("data", timeInfo);
        } catch (Exception e) {
            log.error("获取考试时间信息失败", e);
            result.put("code", 500);
            result.put("msg", "获取时间信息失败：" + e.getMessage());
        }
        return result;
    }
    //获取答卷内容
    @GetMapping("/detail/{answerId}")
    public Map<String, Object> getAnswer(@PathVariable("answerId") Integer answerId) {
        Map<String, Object> result = new HashMap<>();
        try {
            ExamAnswerEntity answer = examAnswerService.getAnswer(answerId);
            result.put("code", 0);
            result.put("msg", "success");
            result.put("answer", answer);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取答卷失败:" + e.getMessage());
            return result;
        }
    }
    //分页查询,好像没有什么用
    @GetMapping("/answer/list")
    public Map<String, Object> list(
            @RequestParam(required = false) Integer pageNo,
            @RequestParam(required = false) Integer pageSize,
            @RequestBody(required = false) ExamAnswerEntity examAnswerEntity) {

        Map<String, Object> result = new HashMap<>();
        try {
            // 如果examAnswerEntity为空，创建一个新的空对象
            if (examAnswerEntity == null) {
                examAnswerEntity = new ExamAnswerEntity();
            }

            // 执行分页查询
            IPage<ExamAnswerEntity> pageResult = examAnswerService.list(examAnswerEntity, pageNo, pageSize);

            // 构建返回结果
            result.put("code", 0);
            result.put("msg", "success");
            result.put("data", pageResult);

        } catch (Exception e) {
            log.error("获取答卷列表失败", e);
            result.put("code", 500);
            result.put("msg", "获取答卷列表失败：" + e.getMessage());
        }
        return result;
    }

    
}